package com.bajiao.service.statistics.impl;

import com.bajiao.entity.order.CouponOrder;
import com.bajiao.mapper.statistics.StatisticsMapper;
import com.bajiao.service.statistics.StatisticsService;
import com.bajiao.vo.statistics.*;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
public class StatisticsServiceImpl extends ServiceImpl<StatisticsMapper, StatisticsOne> implements StatisticsService {

    /**
     * @Description
     * 首页统计
     * @date 2025/1/10 14:22
     * @author FanJq
     * @param
     * @return {@link StatisticsOne}
    */
    @Override
    public StatisticsOne getStatisticsOne() {
        return super.baseMapper.getStatisticsOne();
    }

    /**
     * @Description
     * 曲线图
     * @date 2025/1/10 15:51
     * @author FanJq
     * @param
     * @return {@link List< StatisticsTwo>}
    */
    @Override
    public List<StatisticsTwo> getStatisticsTwo() {
        return super.baseMapper.getStatisticsTwo();
    }

    /**
     * @Description
     * 累计核销量及面额统计
     * @date 2025/1/10 17:57
     * @author FanJq
     * @param
     * @return {@link List< StatisticsThree>}
    */
    @Override
    public List<StatisticsThree> getStatisticsThree() {
        return super.baseMapper.getStatisticsThree();
    }

    /**
     * @Description
     * 下单转化率统计
     * @date 2025/1/10 17:57
     * @author FanJq
     * @param type
     * @return {@link StatisticsFour}
    */
    @Override
    public StatisticsFour getStatisticsFour(Integer type) {
        return super.baseMapper.getStatisticsFour(type);
    }

    /**
     * @Description
     * 券类型销量占比
     * @date 2025/1/10 17:57
     * @author FanJq
     * @param type
     * @return {@link StatisticsFive}
    */
    @Override
    public StatisticsFive getStatisticsFive(Integer type) {
        List<CouponOrder> resultList = super.baseMapper.getStatisticsFive(type);
        StatisticsFive response = new StatisticsFive();
        response.setTotalPay((long) resultList.size());
        response.setCouponOneTotal(resultList.stream().filter(c -> c.getType() == 1).count());
        response.setCouponTwoTotal(resultList.stream().filter(c -> c.getType() == 2).count());
        response.setVerifyTotal(resultList.stream().filter(c -> c.getStatus() == 2 || c.getStatus() == 3).count());
        response.setWaitTotal(resultList.stream().filter(c -> c.getStatus() == 1).count());
        response.setExpireTotal(resultList.stream().filter(c -> c.getStatus() == 4).count());
        if (response.getCouponOneTotal() > 0) {
            String oneRate = new BigDecimal(response.getCouponOneTotal()).divide(new BigDecimal(response.getTotalPay()), 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")).stripTrailingZeros().toPlainString();
            response.setCouponOneRate(oneRate + StringPool.PERCENT);
        }
        if (response.getCouponTwoTotal() > 0) {
            String twoRate = new BigDecimal(response.getCouponTwoTotal()).divide(new BigDecimal(response.getTotalPay()), 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")).stripTrailingZeros().toPlainString();
            response.setCouponTwoRate(twoRate + StringPool.PERCENT);
        }
        return response;
    }

    public static void main(String[] args) {
        new BigDecimal("0").divide(new BigDecimal("1"), 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")).stripTrailingZeros().toPlainString();
        System.out.println();
    }
}
